package com.hhf.cloud.service.impl;

import com.hhf.cloud.apis.AccountFeignApi;
import com.hhf.cloud.apis.StorageFeignApi;
import com.hhf.cloud.entities.Order;
import com.hhf.cloud.mapper.OrderMapper;
import com.hhf.cloud.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.seata.core.context.RootContext;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
 * @author Daniel-黄海峰
 * @Description
 * @create 2024/3/2 15:44
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private StorageFeignApi storageFeignApi;

    @Autowired
    private AccountFeignApi accountFeignApi;

    @Transactional  //ATxt
    @GlobalTransactional(name = "hhf-create-order", rollbackFor = Exception.class)
    @Override
    public void create(Order order) {
        //全局事务id的检查
        String xid = RootContext.getXID();
        log.info("-----------开始新建订单了: " + "\t" + "xid: " + xid);
        //订单新建时默认的状态是零，创建中
        order.setStatus(0);
        int result = orderMapper.insertSelective(order);
        //插入订单成功以后即可获得插入mysql后的实体对象
        Order orderFromDB = null;
        //插入订单成功
        if (result > 0) {
            //从mysql里边查询出来刚插入的记录
            orderFromDB = orderMapper.selectOne(order);
            log.info("--------->新建订单成功，oderFromDB info: " + orderFromDB.toString());
            System.out.println();
            //扣减库存
            log.info("-------> 订单微服务开始调用Storage库存，做扣减count");
            storageFeignApi.decrease(orderFromDB.getProductId(), orderFromDB.getCount());
            log.info("-------> 订单微服务结束调用Storage库存，做扣减完成");
            System.out.println();
            //扣减账户余额
            log.info("-------> 订单微服务开始调用Account账号，做扣减money");
            accountFeignApi.decrease(orderFromDB.getUserId(), orderFromDB.getMoney());
            log.info("-------> 订单微服务结束调用Account账号，做扣减完成");
            System.out.println();
            //修改原始订单状态
            //订单状态status：0：创建中；1：已完结
            log.info("-------> 修改订单状态");
            orderFromDB.setStatus(1);
            //Tkmapper条件
            Example whereCondition = new Example(Order.class);
            Example.Criteria criteria = whereCondition.createCriteria();
            criteria.andEqualTo("userId", orderFromDB.getUserId());
            criteria.andEqualTo("status", 0);
            int updateResult = orderMapper.updateByExampleSelective(orderFromDB, whereCondition);
            log.info("-------> 修改订单状态完成" + "\t" + updateResult);
            log.info("-------> orderFromDB info: " + orderFromDB);
        }
        System.out.println();
        log.info("-----------结束新建订单了: " + "\t" + "xid: " + xid);
    }
}
